﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Runtime.Serialization;
using System.Net;
using System.IO;
using System.Web;
using MS_Enitity.EAP;


namespace MS_UI

{
    public class HikRestInterface
    {
        public delegate void SocketDelegate(int index, string msgStr);

        public event SocketDelegate LogEvent;

        private String serverIp = "";
        private String port = "";
        private String context = "";

        private  FrmMain  form = null;

        private static HikRestInterface hik = null;

        private HikRestInterface(FrmMain clientForm)
        {
            this.form = clientForm;
        }

        public static HikRestInterface getInstance(FrmMain clientForm)
        {
            if (null == hik)
            {
                hik = new HikRestInterface(clientForm);
            }
            return hik;
        }

        public void setServer(String serverIp, String port, String context)
        {
            this.serverIp = serverIp;
            this.port = port;
            this.context = context;
        }

        private String getBaseUrl()
        {
            //return "http://" + serverIp + ":" + port + context;
            return context;



        }

        public String PostMessage(String req, String URI)
        {
            try
            {

                string postData = req; // json.Serialize(paramTask);


                byte[] dataArray = Encoding.UTF8.GetBytes(postData);
                //byte[] dataArray = Encoding.Default.GetBytes(postData);
                HttpWebRequest request = WebRequest.Create(getBaseUrl() +URI) as HttpWebRequest;
                request.Method = "post";
                // request.ContentType = "application/x-www-form-urlencoded";
                request.ContentType = "application/json";
                request.ContentLength = dataArray.Length;
                Stream dataStream = null;
                try
                {
                    dataStream = request.GetRequestStream();
                }
                catch (Exception e)
                {
 
                    LogEvent(1, "调用EAP接口请求失败：" + e.Message);
                    return null;
                }

                //发送请求  
                dataStream.Write(dataArray, 0, dataArray.Length);
                dataStream.Close();
                // Get response
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    // Get the response stream
                    StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.UTF8);

                    // Console application output
                    String s = reader.ReadToEnd();
                    String result = HttpUtility.UrlDecode(s);                  
                    Console.WriteLine(result);
                    //Console.ReadLine();
                    return result;
                }

            }
            catch (Exception e1)
            {
                LogEvent(1, "调用EAP接口请求失败：" + e1.Message);
                return null; 
            }

        }
    }
}
